package service

import (
	"encoding/json"
	"gitlab.yoyiit.com/youyi/go-core/handler"
	"gitlab.yoyiit.com/youyi/go-core/repository"
	"gitlab.yoyiit.com/youyi/go-core/store"
	"gitlab.yoyiit.com/youyi/go-core/util"
	"golang.org/x/net/context"
)

type CounterpartyContactService interface {
	ListCounterpartyContact(ctx context.Context, req *api.ListCounterpartyContactRequest) (*api.ListCounterpartyContactResponse, error)
	GetCounterpartyContact(ctx context.Context, req *api.CounterpartyContactData) (*api.CounterpartyContactData, error)
	AddCounterpartyContact(ctx context.Context, req *api.CounterpartyContactData) error
	EditCounterpartyContact(ctx context.Context, req *api.CounterpartyContactData) error
	DeleteCounterpartyContact(ctx context.Context, id int64) (err error)
	BatchDeleteCounterpartyContact(ctx context.Context, req *api.BatchDeleteCounterpartyContactRequest) (err error)
}

type somsService struct {
	counterpartyContactRepo repo.CounterpartyContactRepo
}

func (s *somsService) ListCounterpartyContact(ctx context.Context, req *api.ListCounterpartyContactRequest) (*api.ListCounterpartyContactResponse, error) {
	dbDatas, count, err := s.counterpartyContactRepo.List(ctx, "", req.PageNum, req.PageSize, &repo.CounterpartyContactDBDataParam{
		CounterpartyContactDBData: repo.CounterpartyContactDBData{
			CounterpartyId: req.Data.CounterpartyId,
			ContactName:    req.Data.ContactName,
			ContactPhone:   req.Data.ContactPhone,
			ContactAddress: req.Data.ContactAddress,
			ProvinceName:   req.Data.ProvinceName,
			ProvinceCode:   req.Data.ProvinceCode,
			CityName:       req.Data.CityName,
			CityCode:       req.Data.CityCode,
			AreaName:       req.Data.AreaName,
			AreaCode:       req.Data.AreaCode,
		},
	})
	if err != nil {
		return nil, handler.HandleError(err)
	}
	var result []*api.CounterpartyContactData
	if dbDatas != nil {
		result = make([]*api.CounterpartyContactData, len(*dbDatas))
		for i, data := range *dbDatas {
			result[i] = &api.CounterpartyContactData{
				Id:                  data.Id,
				OrganizationId:      data.OrganizationId,
				CreatedAt:           util.FormatDateTime(data.CreatedAt),
				UpdatedAt:           util.FormatDateTime(data.UpdatedAt),
				CreatedUserId:       data.CreatedUserId,
				UpdatedUserId:       data.UpdatedUserId,
				UpdatedUserNickname: data.UpdatedUserNickname,
				CreatedUserNickname: data.CreatedUserNickname,
				CounterpartyId:      data.CounterpartyId,
				ContactName:         data.ContactName,
				ContactPhone:        data.ContactPhone,
				ContactAddress:      data.ContactAddress,
				ProvinceName:        data.ProvinceName,
				ProvinceCode:        data.ProvinceCode,
				CityName:            data.CityName,
				CityCode:            data.CityCode,
				AreaName:            data.AreaName,
				AreaCode:            data.AreaCode,
			}
		}
	}
	return &api.ListCounterpartyContactResponse{
		Data:  result,
		Count: count,
	}, nil
}

func (s *somsService) GetCounterpartyContact(ctx context.Context, req *api.CounterpartyContactData) (*api.CounterpartyContactData, error) {
	data, err := s.counterpartyContactRepo.Get(ctx, &repo.CounterpartyContactDBData{
		BaseDBData: repository.BaseDBData{
			BaseCommonDBData: repository.BaseCommonDBData{
				Id: req.Id,
			},
		},
	})
	if err != nil {
		return nil, handler.HandleError(err)
	}
	resp := &api.CounterpartyContactData{
		Id:                  data.Id,
		OrganizationId:      data.OrganizationId,
		CreatedAt:           util.FormatDateTime(data.CreatedAt),
		UpdatedAt:           util.FormatDateTime(data.UpdatedAt),
		CreatedUserId:       data.CreatedUserId,
		UpdatedUserId:       data.UpdatedUserId,
		UpdatedUserNickname: data.UpdatedUserNickname,
		CreatedUserNickname: data.CreatedUserNickname,
		CounterpartyId:      data.CounterpartyId,
		ContactName:         data.ContactName,
		ContactPhone:        data.ContactPhone,
		ContactAddress:      data.ContactAddress,
		ProvinceName:        data.ProvinceName,
		ProvinceCode:        data.ProvinceCode,
		CityName:            data.CityName,
		CityCode:            data.CityCode,
		AreaName:            data.AreaName,
		AreaCode:            data.AreaCode,
	}
	return resp, nil
}

func (s *somsService) AddCounterpartyContact(ctx context.Context, data *api.CounterpartyContactData) error {
	repoData := &repo.CounterpartyContactDBData{
		CounterpartyId: data.CounterpartyId,
		ContactName:    data.ContactName,
		ContactPhone:   data.ContactPhone,
		ContactAddress: data.ContactAddress,
		ProvinceName:   data.ProvinceName,
		ProvinceCode:   data.ProvinceCode,
		CityName:       data.CityName,
		CityCode:       data.CityCode,
		AreaName:       data.AreaName,
		AreaCode:       data.AreaCode,
	}
	_, err := s.counterpartyContactRepo.Add(ctx, repoData)
	if err != nil {
		return handler.HandleError(err)
	}
	return nil
}

func (s *somsService) EditCounterpartyContact(ctx context.Context, data *api.CounterpartyContactData) error {
	repoData := &repo.CounterpartyContactDBData{
		BaseDBData: repository.BaseDBData{
			BaseCommonDBData: repository.BaseCommonDBData{
				Id: data.Id,
			},
		},
		CounterpartyId: data.CounterpartyId,
		ContactName:    data.ContactName,
		ContactPhone:   data.ContactPhone,
		ContactAddress: data.ContactAddress,
		ProvinceName:   data.ProvinceName,
		ProvinceCode:   data.ProvinceCode,
		CityName:       data.CityName,
		CityCode:       data.CityCode,
		AreaName:       data.AreaName,
		AreaCode:       data.AreaCode,
	}
	return handler.HandleError(s.counterpartyContactRepo.UpdateById(ctx, data.Id, repoData))
}

func (s *somsService) DeleteCounterpartyContact(ctx context.Context, id int64) (err error) {
	return handler.HandleError(s.counterpartyContactRepo.DeleteById(ctx, id))
}

func (s *somsService) BatchDeleteCounterpartyContact(ctx context.Context, req *api.BatchDeleteCounterpartyContactRequest) (err error) {
	if req.Ids != nil && len(req.Ids) > 0 {
		// 自行补充逻辑
	}
	return nil
}
